package com.example.authclient.config;

import com.example.authclient.service.impl.RedisClientDetailsService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Configuration
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
    private   static final   Logger logger = LoggerFactory.getLogger(LoginSuccessHandler.class);

    @Value("${myapp.client_name}")
    private String clientName;
    @Value("${myapp.client_auth}")
    private String clientAuth;
    @Value("${myapp.grant_type}")
    private String grantType;
    @Value("${myapp.scope}")
    private String scope;

    @Autowired
    private RedisClientDetailsService redisClientDetailsService;
    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        logger.info("登录成功处理 begin");
        logger.info("认证信息authentication :" +authentication);
        ClientDetails clientDetails= redisClientDetailsService.loadClientByClientId(clientName);
        //获取
        logger.info("获取登录clientDetails： "+clientDetails);
        httpServletRequest.getRequestDispatcher(clientAuth);
        logger.info("登录成功处理 end");
    }
}
